<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .wrap {
            width: 200px;
            height: 200px;
            border: 1px solid red;
        }
    </style>
</head>

<body>
    <div id="app">
        <div :class="{'wrap':flag}">
            <p>姓名：<input type="text" v-model="name"></p>
            <p>年龄：<input type="text" v-model="age"></p>
            <p>
                <select v-model="selected_id" placeholder="请选择性别">
                    <option v-for="(item, index) in sex" :key="index" :value=item.value>{{item.value}}</option>>
                </select>
            </p>
            <p @click="submitfn">{{isadd}}</p>
        </div>
       

        <hr>
        <div v-if="arr.length">
            <li v-for="(item, index) in arr" :key="index">
                <span>{{item.name}}</span>
                <span>{{item.age}}</span>
                <span>{{item.sex}}</span>
                <button @click="delfn(index)">删除</button>
                <button @click="editfn(item,index)">编辑</button>
            </li>
        </div>
        <div v-else>
            请添加一条数据
        </div>

    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                flag:false, // 用于控制弹窗的边框的显示与隐藏
                isadd: "新增",
                editIndex: 0, // 保存编辑第几条的变量
                name: "",
                age: "",
                sex: [{
                    value: "男"
                },
                {
                    value: "女"
                },
                ],
                selected_id: "",
                arr: [
                    {
                        name: "zhangsan",
                        age: "10",
                        sex: "男"
                    },
                    {
                        name: "lisi",
                        age: "11",
                        sex: "女"
                    }
                ]

            },
            methods: { // 放事件处理函数的
                submitfn () {   
                    if (this.isadd === "新增") {
                        this.arr.push({
                            name: this.name,
                            age: this.age,
                            sex: this.selected_id
                        })
                    } else if (this.isadd === "编辑") {
                        this.arr.splice(this.editIndex,1,{
                            name: this.name,
                            age: this.age,
                            sex: this.selected_id   
                        })
                    }


                    this.name = ""
                    this.age = ""
                    this.selected_id = ""
                    this.flag = false

                },
                delfn(index) {
                    this.arr.splice(index, 1)
                },
                editfn(item, index) {
                    console.log(item, index);
                    this.name = item.name
                    this.age = item.age
                    this.selected_id = item.sex
                    this.editIndex = index
                    this.isadd = "编辑"
                    this.flag = true
                }
            }
        })
    </script>
</body>

</html>